package com.dhcc.bpm.alarm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dhcc.bpm.alarm.entity.AlarmRecordDay;
import com.dhcc.bpm.alarm.entity.AlarmRecordMinute;
import com.dhcc.bpm.alarm.service.IAlarmRecordDayService;
import com.dhcc.bpm.alarm.service.IAlarmRecordMinuteService;
import com.dhcc.bpm.alarm.service.IAlarmTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class AlarmTaskServiceImpl implements IAlarmTaskService {
	
	//@Autowired
	//AlarmRecordMinuteMapper alarmTaskMapper;
	@Autowired
	IAlarmRecordMinuteService alarmRecordMinuteService;
	@Autowired
	IAlarmRecordDayService alarmRecordDayService;
	
	/**
	 ** 定时插入告警记录数量-按分钟 
	 *@author coayanlong
	 */
	@Override
	@Transactional
	public JSONObject insertRecordMinutes(JSONObject taskParam) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, -5);
		Date beforeDate = beforeTime.getTime();
		String time = sdf.format(beforeDate)+"%";
		
		AlarmRecordMinute AlarmRecordMinute = new AlarmRecordMinute();
		AlarmRecordMinute.setAlarmDateTime(time);
		List<AlarmRecordMinute> alarmMinuteList = alarmRecordMinuteService.findListByMinutes(AlarmRecordMinute);
		
		for(AlarmRecordMinute alarmMinute : alarmMinuteList) {
			alarmMinute.setNoticeMethodName(NoticeMethod.getName(alarmMinute.getNoticeMethod()));
			
			String alarmDateTime = alarmMinute.getAlarmDateTime()+"00";
			String alarmDate = alarmDateTime.substring(0,8);
			String alarmTime = alarmDateTime.substring(8);
			
			alarmMinute.setAlarmDateTime(alarmDateTime); // 时间后面补两位秒。
			alarmMinute.setAlarmDate(alarmDate);
			alarmMinute.setAlarmTime(alarmTime);
			alarmMinute.setCreateTime(new Date());
			
			alarmRecordMinuteService.save(alarmMinute);
		}
		
		JSONObject result = new JSONObject();
		result.put("code", "0000");
		result.put("msg", "同步完成("+alarmMinuteList.size()+")");
		return result;
	}
	
	@Transactional
	@Override
	public JSONObject insertRecordDays(JSONObject taskParam) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.DAY_OF_MONTH, -1);
		Date beforeDate = beforeTime.getTime();
		String time = sdf.format(beforeDate)+"%";
		
		AlarmRecordDay alarmRecordDay = new AlarmRecordDay();
		alarmRecordDay.setAlarmDate(time);
		List<AlarmRecordDay> alarmDayList = alarmRecordDayService.findListByDays(alarmRecordDay);
		
//		int batchNum = 5; // 每batchNum插入一次
//		List<AlarmRecordDay> insertList = new ArrayList<AlarmRecordDay>();
//		
//		for(int i=0; i< alarmDayList.size(); i++ ) {
//			
//			AlarmRecordDay alarmDay = alarmDayList.get(i);
//			alarmDay.setNoticeMethodName(NoticeMethod.getName(alarmDay.getNoticeMethod()));
//			alarmDay.setCreateTime(new Date());
//			
//			if((i+1) == batchNum || i == alarmDayList.size()-1 ) {
//				int insertNum = alarmRecordDayService.insertDayBatch(insertList);
//				log.info("批量插入:{}", insertNum);
//				insertList = new ArrayList<AlarmRecordDay>();
//			}
//		}
		alarmRecordDayService.saveBatch(alarmDayList, 100);
		
		JSONObject result = new JSONObject();
		result.put("code", "0000");
		result.put("msg", "同步完成("+alarmDayList.size()+")");
		return result;
		
	}
	
	public static enum NoticeMethod {
		SMS("01", "短信"),
		WECHAT("02", "微信"),
		WECHAT_ES("03", "企业微信"),
		API("04", "API"),
		H5("05", "H5");
		
		private String code;
		private String name;
		
		NoticeMethod(String code, String name) {
			this.code = code;
			this.name = name;
		}
		
		public static String getName(String code) {
			NoticeMethod[] enums = NoticeMethod.values();
	        for (NoticeMethod em : enums) {
	            if (em.getCode().equals(code)) {
	                return em.name;
	            }
	        }
	        return "其它";
	    }
		
		public String getCode() {
			return code;
		}
		
	}

	
}
